/*
 * Background.cpp
 *
 *  Created on: 30/05/2014
 *      Author: lucio
 */

#include "Background.h"
#include <cairomm/context.h>
#include <gdkmm/general.h>
#include <glibmm/fileutils.h>
#include <iostream>

bool Background::on_draw(const Cairo::RefPtr<Cairo::Context>& cr){
	if (!background)
	  return false;

	Gtk::Allocation allocation = get_allocation();
	const int width = allocation.get_width();
	const int height = allocation.get_height();

	// Draw the image in the middle of the drawing area, or (if the image is
	// larger than the drawing area) draw the middle part of the image.
	Gdk::Cairo::set_source_pixbuf(cr, background,
	  (width - background->get_width())/2, (height - background->get_height())/2);
	cr->paint();

	return true;
}

Background::Background(){
	  try
	  {
	    // The fractal image has been created by the XaoS program.
	    // http://xaos.sourceforge.net
		  background = Gdk::Pixbuf::create_from_file("fractal_image.png");
	  }
	  catch(const Glib::FileError& ex) {
	    std::cerr << "FileError: " << ex.what() << std::endl;
	  }
	  catch(const Gdk::PixbufError& ex) {
	    std::cerr << "PixbufError: " << ex.what() << std::endl;
	  }

	  // Show at least a quarter of the image.
	  if (background)
	    set_size_request(background->get_width()/2, background->get_height()/2);
}

Background::~Background() {
	// TODO Auto-generated destructor stub
}

